<script lang="ts" setup>
import { Unlock, User } from '@element-plus/icons-vue';
import bg from '@/assets/image/login-bg.jpg';
import { useUserStore } from '@/stores/user';

const bgImage = ref(bg);
const loginForm = reactive({
  username: 'admin',
  password: 'admin',
});
const loginRules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 5, max: 15, message: '账号长度在5-15之间', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '密码长度在6-18之间', trigger: 'blur' },
  ],
});

const userStore = useUserStore();

const onLogin = () => {
  userStore.login(loginForm).then(() => {
    userStore.$patch((state: any) => {
      state.isLogin = true;
    });
  });
};
</script>

<template>
  <div class="login-container h-screen w-full overflow-hidden">
    <img :src="bgImage" alt="" style="width: 100%; height: 100%" />
    <el-form
      label-position="right"
      :model="loginForm"
      :rules="loginRules"
      size="large"
      class="login-form"
    >
      <div class="login-title">礼上来往</div>
      <el-form-item prop="username">
        <el-input
          placeholder="请输入账号"
          v-model="loginForm.username"
          clearable
          @keyup.enter="onLogin"
        >
          <template #prefix>
            <el-icon :size="28">
              <user />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="请输入密码"
          type="password"
          v-model="loginForm.password"
          clearable
          show-password
          @keyup.enter="onLogin"
        >
          <template #prefix>
            <el-icon :size="28">
              <unlock />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-button
        type="primary"
        class="login-btn"
        @click="onLogin"
        @keyup.enter="onLogin"
      >登录</el-button
      >
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  .login-title {
    color: #22c3aa;
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 40px;
  }

  .login-form {
    background-color: rgba(249, 250, 252, 0.8);
    border-radius: 15px;
    overflow: hidden;
    padding: 40px;
    width: 480px;
    height: 420px;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    text-align: center;

    .el-form-item {
      margin-bottom: 40px;

      ::v-deep(.el-input__inner) {
        border: 0;
        height: 50px;
      }

      ::v-deep(.el-input__wrapper) {
        box-shadow: 4px 4px 1px #f3f5f7;
      }
    }

    .login-btn {
      border: 0;
      width: 180px;
      height: 48px;
      background-color: #22c3aa;
      box-shadow: 4px 4px 1px #d8d4f4;
      font-size: 16px;
    }
  }
}
</style>
